from enum import Enum
from typing import Union, Literal

import requests
from ..setting import USER_AGENT

requests.packages.urllib3.disable_warnings()


class BananaParam(Enum):
    # :param definition: 清晰度 0-全部 1-360P 2-480P 3-720P 4-1080P
    # :param duration: 规格, 1-长片 2-短片
    # :param freetype: 收费类型 0-免费 1-会员
    # :param mosaic: 无码/有码 0-全部 1-有码 2-无码
    definition = {
        '0': '全部',
        '1': '360P',
        '2': '480P',
        '3': '720P',
        '4': '1080P'
    }
    duration = {
        '0': '全部',
        '1': '长片',
        '2': '短片'
    }
    freetype = {
        '0': '免费',
        '1': '会员'
    }
    mosaic = {
        '0': '全部',
        '1': '有码',
        '2': '无码'
    }

    def parse_param(self, param: Union[str, int]) -> str:
        if param is None:
            return '未知'
        v = self.value.get(str(param), '未知')
        return v


class BananaCategory(Enum):
    CATEGORYS = [
        {
            "keyid": "16",
            "value": "香蕉原创"
        },
        {
            "keyid": "5",
            "value": "制服诱惑"
        },
        {
            "keyid": "6",
            "value": "清纯少女"
        },
        {
            "keyid": "7",
            "value": "辣妹大奶"
        },
        {
            "keyid": "8",
            "value": "女同专属"
        },
        {
            "keyid": "9",
            "value": "素人出演"
        },
        {
            "keyid": "10",
            "value": "角色扮演"
        },
        {
            "keyid": "11",
            "value": "成人动漫"
        },
        {
            "keyid": "12",
            "value": "人妻熟女"
        },
        {
            "keyid": "13",
            "value": "变态另类"
        },
        {
            "keyid": "14",
            "value": "经典伦理"
        },
        {
            "keyid": "4",
            "value": "偷拍自拍"
        }
    ]
    AREAS = [
        {
            "keyid": "2",
            "value": "国产",
        },
        {
            "keyid": "3",
            "value": "日本",
        },
        {
            "keyid": "8",
            "value": "台湾省"
        },
        {
            "keyid": "5",
            "value": "韩国"
        },
        {
            "keyid": "7",
            "value": "香港"
        },
        {
            "keyid": "6",
            "value": "欧美"
        }
    ]
    YEARS = [
        {
            "keyid": "12",
            "value": "2025",

        },
        {
            "keyid": "6",
            "value": "2019"
        },
        {
            "keyid": "3",
            "value": "2018"
        },
        {
            "keyid": "5",
            "value": "2010-2017"
        },
        {
            "keyid": "4",
            "value": "00年代"
        },
        {
            "keyid": "1",
            "value": "90年代"
        }
    ]
    DEFINITIONS = [
        {
            "keyid": 1,
            "value": "标清"
        },
        {
            "keyid": 2,
            "value": "高清"
        }
    ]
    DURATIONS = [
        {
            "keyid": 1,
            "value": "长片"
        },
        {
            "keyid": 2,
            "value": "短片"
        }
    ]
    FREETYPES = [
        {
            "keyid": 1,
            "value": "免费"
        },
        {
            "keyid": 2,
            "value": "会员"
        }
    ]
    MOSAICS = [
        {
            "keyid": 1,
            "value": "有码"
        },
        {
            "keyid": 2,
            "value": "无码"
        }
    ]
    LANGVOICES = [
        {
            "keyid": 1,
            "value": "中文字幕"
        },
        {
            "keyid": 2,
            "value": "国语对白"
        },
        {
            "keyid": 3,
            "value": "其它"
        }
    ]
    ORDERBYS = [
        {
            "keyid": 1,
            "value": "最多好评"
        },
        {
            "keyid": 2,
            "value": "最多播放"
        },
        {
            "keyid": 3,
            "value": "最高评分"
        }
    ]


class BananaServer:
    base_url = 'https://osbkypuuqtal.xzjgz.com'

    @classmethod
    def send_message(cls, url: str, isAdd: bool = True, **kwargs):
        if isAdd:
            url = cls.base_url + url
        headers = {'User-Agent': USER_AGENT}
        response = requests.get(url, headers=headers, timeout=10, verify=False, **kwargs)
        response.raise_for_status()
        response.encoding = 'utf-8'
        return response.json()

    @classmethod
    def home(cls):
        """
        首页
        :return:
        """
        return cls.send_message('/index')

    @classmethod
    def listing(cls, page=1):
        """
        频道
        :return:
        """
        return cls.send_message(f'/special/listing-0-0-{page}')

    @classmethod
    def detail(cls, spid: Union[str, int]):
        """
        详情
        :param spid:
        :return:
        """
        return cls.send_message(f'/special/detail/{spid}')

    @classmethod
    def vod(cls, tp: Literal['recommend', 'hot', 'latest'], page=1):
        """
        推荐、热点、最新
        :param tp:
        :param page:
        :return:
        """
        return cls.send_message(f'/vod/{tp}-0-0-0-0-0-0-0-0-0-{page}')

    @classmethod
    def vod_listing(
            cls,
            cateid=0,
            areaid=0,
            yearid=0,
            definition=0,
            duration=0,
            mosaic=0,
            langvoice=0,
            orderby=0,
            page=1
    ):
        """
        视频详情
        /vod/listing-{全部分类}-{全部地区}-{全部年份}-{清晰度}-{规格}-{收费类型}-{无码|有码}-{语言}-{综合排序}-{页码}
        -----------------------
        :param cateid: 分类ID
        :param areaid: 地区ID
        :param yearid: 年份ID
        :param definition: 清晰度 0-全部 1-360P 2-480P 3-720P 4-1080P
        :param duration: 规格, 1-长片 2-短片
        # :param freetype: 收费类型 0-免费 1-会员
        :param mosaic: 无码/有码 0-全部 1-有码 2-无码
        :param langvoice: 语言
        :param orderby: 综合排序
        :param page: 页码
        :return:
        """
        data = cls.send_message(
            f'/vod/listing-{cateid}-{areaid}-{yearid}-{definition}-{duration}-0-{mosaic}-{langvoice}-{orderby}-{page}')
        return data

    @classmethod
    def search(cls, wd: str, page=1):
        """
        搜索
        :param wd:
        :param page:
        :return:
        """
        return cls.send_message(f'/search?page={page}&wd={wd}')

    @classmethod
    def minivod(cls, page=1):
        data = cls.send_message('/minivod/reqlist', data={'page': page})
        return data

    @classmethod
    def replay(cls, vodid: Union[str, int], isMin=False) -> str:
        """
        重置播放地址
        :param vodid:
        :return:
        """
        if isMin:
            url = f'/minivod/reqplay/{vodid}'
        else:
            url = f'/vod/reqplay/{vodid}'
        result = cls.send_message(url)
        retcode = result.get('retcode')
        if retcode is None:
            return ''
        if retcode == 0:
            return result['data']['httpurl']
        elif retcode == 3:
            return result['data']['httpurl_preview']
        elif retcode == 6:
            raise ValueError(result['errmsg'])

        http_url = result['data']['httpurl']
        res = cls.send_message(http_url).text.strip().split('\n')
        return cls.base_url + res[-1]

    @classmethod
    def search_recommended(cls):
        """
        推荐搜索
        :return:
        """
        url = 'https://ciaobo0ixnab.nzx892fl8n.com/search'
        return cls.send_message(url, isAdd=False)
